package com.gxa.meiye.portal.controller.lzj;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.pojo.domain.Consumer;
import com.gxa.meiye.core.pojo.domain.Order;

import com.gxa.meiye.portal.hread.UserThreadLocal;
import com.gxa.meiye.portal.service.ShopOrderService;
import com.gxa.meiye.portal.utils.PayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.UUID;

@Api(value = "Order1",tags = "到店服务订单表接口")
@Controller

public class OrderController {
    @Autowired
    private ShopOrderService orderService;
    @Autowired
    private PayUtil payUtil;
    //创建订单
    @ResponseBody
    @ApiOperation(value = "添加到店订单",notes = "添加")
    @PostMapping("/order/add")
    public Map addOrder(Order order) throws JsonProcessingException {
        System.out.println(order);
        //从本地线程池获取当前消费者电话
        Object o = UserThreadLocal.get();
        ObjectMapper objectMapper=new ObjectMapper();
        Consumer consumer = objectMapper.readValue(o.toString(), Consumer.class);
        String phone = consumer.getPhone();
        System.out.println(phone);
        //String phone=null;
        //订单编号
        String s = "meiye"+ UUID.randomUUID().toString().replaceAll("-", "");
        order.setOrderNumber(s);
        boolean b = orderService.orderAdd(phone, order);
        JsonResult jsonResult=new JsonResult();
//        String pay = payUtil.pay(order.getGetMoney().toString());
//
//
//        if (!StringUtils.isEmpty(pay)){
//            boolean update = orderService.update(s,pay);
//            jsonResult.setCode("1");
//            if (update) {
//
//                jsonResult.setMsg("付款成功,已支付订单");
//            }else {
//                jsonResult.setMsg("状态修改失败");
//
//            }
//            return jsonResult.getMap();
//        }
        jsonResult.setCode("2");
        jsonResult.setMsg("创建成功");
        return jsonResult.getMap();
    }



}
